LIBRARY ieee;
use std.textio.all;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
USE ieee.std_logic_textio.all;

PACKAGE MEM_FILE IS
        SUBTYPE sword IS STD_LOGIC_VECTOR(7 DOWNTO 0);
        TYPE word_array IS ARRAY (0 to 255) of sword;
        FUNCTION READ_MEMORY_FILE(mem : STRING) RETURN word_array;
END MEM_FILE;

PACKAGE BODY MEM_FILE IS
        FUNCTION READ_MEMORY_FILE(mem : STRING) RETURN word_array IS
                FILE input          : TEXT OPEN READ_MODE IS mem;
                VARIABLE input_line : LINE;
                VARIABLE input_addr : STD_LOGIC_VECTOR(7 DOWNTO 0);
                VARIABLE input_data : STD_LOGIC_VECTOR(31 DOWNTO 0);
                VARIABLE lines	    : INTEGER RANGE 0 TO 128 := 0;
                VARIABLE word_list  : word_array := (OTHERS => sword'(X"00"));
	BEGIN
                WHILE lines < 128 AND NOT ENDFILE(input) LOOP
                            READLINE( input, input_line );
                            HREAD( input_line, input_addr );
                            HREAD( input_line, input_data );
        
                            word_list( CONV_INTEGER( input_addr )*4+0 ) := input_data(31 DOWNTO 24);
	        	    word_list( CONV_INTEGER( input_addr )*4+1 ) := input_data(23 DOWNTO 16);
	                    word_list( CONV_INTEGER( input_addr )*4+2 ) := input_data(15 DOWNTO 8);
	        	    word_list( CONV_INTEGER( input_addr )*4+3 ) := input_data(7 DOWNTO 0);
	                    lines := lines + 1;
                END LOOP;
                RETURN word_list;
        END READ_MEMORY_FILE;
END MEM_FILE;
